<?php

class PhpTokenizer
{
    static protected $out;
    static protected $tokens;

    static public function compact($source)
    {
        self::$tokens = token_get_all($source);
        self::$out = '';

        reset(self::$tokens);

        while ($t = current(self::$tokens)) {
            if (is_array($t)) {
                if ($t[0] == T_WHITESPACE || $t[0] == T_DOC_COMMENT || $t[0] == T_COMMENT) {
                    self::skipWhiteAndComments();
                    continue;
                }
                self::$out .= $t[1];
            } else {
                self::$out .= $t;
            }

            next(self::$tokens);
        }

        return self::$out;
    }

    static private function skipWhiteAndComments()
    {
        self::$out .= ' ';
        while ($t = current(self::$tokens)) {
            if (is_array($t) && ($t[0] == T_WHITESPACE || $t[0] == T_DOC_COMMENT || $t[0] == T_COMMENT)) {
                next(self::$tokens);
            } else {
                return;
            }
        }
    }

    static public function addTokenType()
    {
        $args = func_get_args();
        // 添加要处理的Token类型
        if(!empty($args)){
            foreach($args as $type){
                array_unshift(self::$tokenTypes, $type);
            }
        }
    }

    static public function strip($source)
    {
        self::$tokens = token_get_all($source);
        self::$out = '';
        self::addTokenType(T_COMMENT, T_DOC_COMMENT);

        foreach(self::$tokens as $t)
        {
            if(is_array($t)){
                if (in_array($t[0], self::$tokenTypes))
                {
                    self::$out .= '';
                }else{
                    self::$out .= $t[1];
                }
            }else{
                self::$out .= $t;
            }
        }
        self::$out = preg_replace("'([\r\n])[\s]+'", "\n", self::$out); //合并多个空行
        return self::$out;
    }


}
?>